package com.yyl.rbacs.sys.controller;

import com.baomidou.kaptcha.Kaptcha;
import com.yyl.rbacs.sys.entity.SysUser;
import com.yyl.rbacs.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @BelongsProject: rbacs
 * @BelongsPackage: com.yyl.rbacs.controller
 * @Author: YuanSir
 * @CreateTime: 2022-10-28  14:49
 * @Description:
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/sys")
public class SysLogController {
    
    @Autowired
    private Kaptcha kaptcha;
    
    @PostMapping("/login")
    public R login(SysUser user, String captcha){
        
        log.debug("captcha={}", captcha);
    
        //验证码处理
        try {
            kaptcha.validate(captcha);
        } catch (Exception e){
    
            return R.error("验证码错误！");
    
        }
        
        //2.用户身份信息比对
        Subject subject = SecurityUtils.getSubject();
    
        try {
            //shiro的登录方法
            subject.login(new UsernamePasswordToken(user.getUsername(), user.getPassword()));
        } catch (Exception e){
            e.printStackTrace();
            return R.error("登陆失败，请检查用户名和密码！");
        }
        
        return R.ok("登陆成功！");
    
    }
    
}
